import Axios from 'axios'
import * as constants from './constants'
import { fromJS } from 'immutable'

export const getHomeInfo = () => {
  return  dispatch => {
    Axios.get('/api/home.json').then(res => {
      const result = res.data.data;
      const action = {
        type: constants.CHANGE_HOME,
        topicList: result.topicList,
        articleList: result.articleList,
        recommendList: result.recommendList
      }
      dispatch(action)
    }).catch(err => {
      console.log(err)
    })
  }
}

export const getMoreList = (page) => {
  return dispatch => {
    Axios.get(`/api/homeList.json?page=${page}`).then(res => {
      const result = res.data.data;
      const action = {
        type: constants.ADD_ARTICLE_LIST,
        nextPage: page + 1,
        list: fromJS(result)
      }
      dispatch(action)
    }).catch(err => {
      console.log(err)
    })
  }
}

export const toggleTopShow = (show) => ({
  type: constants.TOGGLE_SCROLL_TOP,
  show
}) 